from base.shared.JenaException import JenaException


class IStream:

    # simple input stream handler

    reader = None
    thisChar = ''
    eof = False
    # int 
    charpos = 1
    # int 
    linepos = 1

    def __init__(self, reader):
        try:
            self.reader = reader
            eof = (reader.read(self.thisChar, 0, 1) == -1)
        except Exception as e:
            raise JenaException(e)
        
    

    def readChar(self):
        try:
            if (self.eof):
                return '\000'
            # char
            rv = self.thisChar[0]
            eof = (self.reader.read(self.thisChar, 0, 1) == -1)
            if (rv == '\n'):
                self.linepos += 1
                self.charpos = 0
            else:
                self.charpos += 1
            
            return rv
        except Exception as e:
            raise JenaException(e)
        
    

    def nextChar(self):
        return '\000' if self.eof else self.thisChar[0]
    

    # result boolean
    def getEof(self):
        return self.eof
    

    # result int
    def getLinepos(self):
        return self.linepos
    

    # result protected int
    def getCharpos(self):
        return self.charpos
    

